package net.kazed.nextaction.timer;

import net.kazed.nextaction.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;

public class Notifier {

	private static final int NOTIFICATION_SERVICE_STARTED = R.string.process_timer_running_title;

	private Context context;

	/**
	 * Constructor.
    * @param context Android context.
	 */
	public Notifier(Context context) {
	   this.context = context;
	}

	/**
	 * Timer started.
    * @param notificationIntent Intent for use in notification.
	 */
	public void timerStart(Intent notificationIntent) {
		displayNotification(notificationIntent, 0, R.string.process_timer_running_title, R.string.process_timer_running_text,
		      Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT);
	}

	/**
	 * Timer stopped.
	 * @param notificationIntent Intent for use in notification.
	 */
	public void timerStop(Intent notificationIntent) {
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.cancel(NOTIFICATION_SERVICE_STARTED);
      displayNotification(notificationIntent, R.string.process_timer_stopped_ticker, R.string.process_timer_stopped_title, R.string.process_timer_stopped_text,
            Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_SOUND);
	}

	private void displayNotification(Intent notificationIntent, int tickerResource, int titleResource, int textResource, int flags) {
      Resources resources = context.getResources();
      int icon = R.drawable.next_action_icon2;
      String tickerText = null;
      if (tickerResource != 0) {
         tickerText = resources.getString(tickerResource);
      }
      long when = System.currentTimeMillis();
      String contentTitle = resources.getString(titleResource);
      CharSequence contentText = resources.getString(textResource);

      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

      // the next two lines initialize the Notification, using the configurations above
      Notification notification = new Notification(icon, tickerText, when);
      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
      notification.flags |= flags;
      
      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
      notificationManager.notify(NOTIFICATION_SERVICE_STARTED, notification);
   }
   
}
